package com.itheima.reggie.common;

import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpSession;

/**
 * 基于ThreadLocal封装工具类，用户保存和获取当前登录用户id
 */
@Slf4j
public class BaseContext {
    private static ThreadLocal<HttpSession> threadLocal = new ThreadLocal<>();

    /**
     * 设置值
     *
     */
    public static void setCurrentId(HttpSession session) {
        threadLocal.set(session);
    }

    /**
     * 获取值
     *
     * @return
     */
    public static Long getCurrentId() {
        log.info("当前普通用户id：{}", threadLocal.get().getAttribute("user"));
        log.info("当前管理员用户id：{}", threadLocal.get().getAttribute("employee"));
        return (Long) threadLocal.get().getAttribute("user");
    }

    public static Long getAdminId() {
        return (Long) threadLocal.get().getAttribute("employee");
    }
}
